﻿function distribution() {
	var $this = $(this);
	var proId = $this.attr('productId');
	$.post('/malladmin/distribution/adddistributionproductset',{productId:proId},function(result) {
		if (result.success) {
			setDistributionRow($this.parent().parent(), result.data);
		} else {
			alert(result.message);
		}
	});
	function setDistributionRow($row,data) {
		var $td = $row.find('td:eq(3)');
		$td.empty();
		var $select = $('<select class="pumpedType"><option value="Fixed">固定</option><option value="Percentage">百分比</option></select>');
		$select.val(data.type);
		$td.html($select);

		$td = $row.find('td:eq(4)');
		$td.empty();
		var $number = $('<input class="pumpedValue" type="number" />');
		$number.val(data.value);
		$td.html($number);

		$td = $row.find('td:last');
		$td.find('.distribution').hide();
		$td.find('.saveSet').show();
		$td.find('.cancelDistribution').show();
	}
}
function saveSet() {
	var $this = $(this);
	var data = {
		id: $this.attr('setId'),
		pumpedType: $this.parent().parent().find('.pumpedType').val(),
		pumpedValue: $this.parent().parent().find('.pumpedValue').val(),
		productId: $this.attr('productId')
	};
	$.post('/malladmin/distribution/savedistributionproductset',data,function(result) {
		if (result.success) {

		} else {
			alert(result.message);
		}
	});
}
function cancelDistribution() {
	var $this = $(this);
	var id = $this.attr('setId');
	$.post('/malladmin/distribution/canceldistributionproductset', { id: id }, function(result) {
		if (result.success) {
			setUnDistributionRow($this.parent().parent());
		} else {
			alert(result.message);
		}
	});
	function setUnDistributionRow($row) {
		var $td = $row.find('td:eq(3)');
		$td.empty();
		$td.text('未设置');

		$td = $row.find('td:eq(4)');
		$td.empty();
		$td.text('未设置');

		$td = $row.find('td:last');
		$td.find('.distribution').show();
		$td.find('.saveSet').hide();
		$td.find('.cancelDistribution').hide();
	}
}

$(function() {
	$('.distribution').bind('click',distribution);
	$('.saveSet').bind('click',saveSet);
	$('.cancelDistribution').bind('click', cancelDistribution);
});